﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using TestReflectInterface;

namespace TestEffectiveCS
{
    class Test43
    {


        public Test43()
        {
            Assembly assembly = Assembly.LoadFrom("TestReflect.dll");

            foreach (Type type in assembly.GetTypes())
            {
                Console.WriteLine("Type name : " + type.Name);

                foreach (PropertyInfo property in type.GetProperties())
                {
                    Console.WriteLine("PropertyInfo name : " + property.Name);
                }

                foreach (MethodInfo method in type.GetMethods())
                {
                    Console.WriteLine("MethodInfo name : " + method.Name);
                }

                foreach (ConstructorInfo constructor in type.GetConstructors())
                {
                    Console.WriteLine("ConstructorInfo name : " + constructor.Name);
                    foreach (ParameterInfo parameter in constructor.GetParameters())
                    {
                        Console.WriteLine("ConstructorInfo ParameterInfo name : " + parameter.Name);
                        Console.WriteLine("ConstructorInfo ParameterInfo type : " + parameter.ParameterType.FullName);
                    }
                }

                //获取无参构造
                ConstructorInfo myTypeConstructor = type.GetConstructor(new Type[0]);
                //获取带参构造
                ConstructorInfo myTypeConstructor1 = type.GetConstructor(new Type[1] { typeof(string) });


                //用接口代替反射
                IMyInterface myInterface = myTypeConstructor.Invoke(null) as IMyInterface;
                

                myInterface.Message = "MyType to IMyInterface";
                myInterface.DoWork();

                IMyInterface myInterface1 = myTypeConstructor1.Invoke(new object[1]{"MyType to IMyInterface"}) as IMyInterface;

                myInterface1.DoWork();
            }
        }
    }
}
